package hasthi.container.p2p;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.w3.x2003.x05.soapEnvelope.EnvelopeDocument;

import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.NodeHandle;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.MessageDeserializer;



public class XmlMessageDeserializer implements MessageDeserializer  {

    public Message deserialize(InputBuffer inbuf, short arg1, int arg2, NodeHandle arg3) throws IOException {
        try {
            int remaingbytes = inbuf.bytesRemaining();
            byte[] data = new byte[remaingbytes];
            inbuf.read(data);
            XmlObject results = XmlObject.Factory.parse(new InputStreamReader(new ByteArrayInputStream(data)));
            return new UnicastXmlMessage((EnvelopeDocument)results);
        } catch (XmlException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    }

}

